<?php

require_once("database.php");
require_once("util.php");
require_once("cookie.php");

class User
{
	private $id;
	private $username;
	private $password;
	private $address;
	private $email;
	private $regTime;
	
	function __construct($id = 0,
						 $username = "",
						 $password = "",
						 $address = "",
						 $email = "",
						 $regTime = "")
	{
		$this->id = $id;
		$this->username = $username;
		$this->password = $password;
		$this->address = $address;
		$this->email = $email;
		$this->registerTime = $regTime;
	}
	
	public function setId($id) {$this->id = $id;}
	public function setUsername($username) {$this->username = $username;}
	public function setPassword($password) {$this->password = $password;}
	public function setAddress($address) {$this->address = $address;}
	public function setEmail($email) {$this->email = $email;}
	public function setRegTime($regTime) {$this->regTime = $regTime;}
	
	public function getId() {return $this->id;}
	public function getUsername() {return $this->username;}
	public function getPassword() {return $this->password;}
	public function getAddress() {return $this->address;}
	public function getEmail() {return $this->email;}
	public function getRegTime() {return $this->regTime;}
}





class UserDAO
{
	private $db;
	function __construct()
	{
		$this->db = new Database();
	}
	
	public function addUser($user)
	{
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("insert into ESUser(name, pass, email, address, regDate) values(?, ?, ?, ?, ?)",
												array($user->getUsername(), $user->getPassword(), $user->getEmail(), $user->getAddress(), Util::getCurrentDate()));
			$prepStmt->executeUpdate();
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
	}
	
	public function userExist($user)
	{
		$rslt = false;
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("select * from ESUser where name = ?", array($user->getName()));
			$rs = $prepStmt->executeQuery();
			if($rs->next())
				$rslt = true;
			else
				$rslt = false;
		}
		catch(SQLException $e)
		{
			$rslt = 2;
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $rslt;
	}
	
	public function modifyPassword($userId, $oldPass, $newPass)
	{
		$rslt = 0;
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("select * from ESUser where id = ?", array($userId));
			$rs = $prepStmt->executeQuery();
			if($rs->next())
			{
				$pass = $rs->value("pass");
				if($pass = $oldPass)
				{
					$prepStmt = $conn->prepareStatement("update ESUser set pass = ? where id = ?", array($newPass, $userId));
					$prepStmt->executeUpdate();
				}
				else
				{
					$rslt = 1;
				}
			}
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$rslt = 2;
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $rslt;
	}
	
	public function modifyEmail($userId, $email)
	{
		$rslt = true;
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("update ESUser set email = ? where id = ?", array($email, $userId));
			$prepStmt->executeUpdate();
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$rslt = false;
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $rslt;
	}
	
	public function modifyAddress($userId, $address)
	{
		$rslt = true;
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("update ESUser set address = ? where id = ?", array($address, $userId));
			$prepStmt->executeUpdate();
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$rslt = false;
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $rslt;
	}
	
	function login($username, $password)
	{
		$rslt = 0;
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("select * from ESUser where name = ?", array($username));
			$rs = $prepStmt->executeQuery();
			if($rs->next())
			{
				$pass = $rs->value("pass");
				if($pass == $password)
				{
					$rslt = $rs->value("id");
				}
			}
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$rslt = false;
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $rslt;
	}
	
	static public function isLoggedIn()
	{
		return CookieProvider::cookieExist(Config::COOKIE_USERID) && CookieProvider::cookieExist(Config::COOKIE_USERNAME);
	}
}

?>